package com.hruiworks.campusgroup.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hruiworks.campusgroup.common.PageVO;
import com.hruiworks.campusgroup.dao.mapper.ActivityMessageMapper;
import com.hruiworks.campusgroup.enums.ErrorCode;
import com.hruiworks.campusgroup.exception.BusinessException;
import com.hruiworks.campusgroup.pojo.entity.Activity;
import com.hruiworks.campusgroup.pojo.entity.ActivityMessage;
import com.hruiworks.campusgroup.pojo.entity.User;
import com.hruiworks.campusgroup.service.ActivityMessageService;
import com.hruiworks.usercheck.util.ParamUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
* @author jiushui
* @description 针对表【activity_message(活动上线消息)】的数据库操作Service实现
* @createDate 2025-04-09 18:44:46
*/
@Service
public class ActivityMessageServiceImpl extends ServiceImpl<ActivityMessageMapper, ActivityMessage>
    implements ActivityMessageService{

    @Resource
    private ActivityMessageMapper activityMessageMapper;

    @Override
    public Boolean releaseActivityMessage(Activity activity) {
        ActivityMessage activityMessage = new ActivityMessage();
        activityMessage.setActivityId(activity.getId());
        activityMessage.setActivityName(activity.getActivityName());
        activityMessage.setUniversity(activity.getUniversity());

        activityMessage.setMessage("【活动上新】"+activity.getActivityName()+"活动上线了！！");

        int insert = activityMessageMapper.insert(activityMessage);
        return insert == 1;
    }

    @Override
    public PageVO<ActivityMessage> listPageActivityMessage(Long current, Long pageSize, User user) {
        if (ParamUtils.isAnyBlank(current, pageSize, user)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数有误或不全");
        }

        PageVO<ActivityMessage> activityMessagePageVO = new PageVO<>();


        Page<ActivityMessage> queryPage = new Page<>(current, pageSize);
        LambdaQueryWrapper<ActivityMessage> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ActivityMessage::getUniversity, user.getUniversity());
        lambdaQueryWrapper.orderByDesc(ActivityMessage::getId);

        Page<ActivityMessage> activityMessagePage = activityMessageMapper.selectPage(queryPage, lambdaQueryWrapper);

        activityMessagePageVO.setRecords(activityMessagePage.getRecords());
        activityMessagePageVO.setTotal(activityMessagePage.getTotal());
        activityMessagePageVO.setSize(activityMessagePage.getSize());
        activityMessagePageVO.setCurrent(activityMessagePage.getCurrent());

        return activityMessagePageVO;
    }
}




